package com.cloudkinto.service.company.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.CompanyDao;
import com.cloudkinto.dao.ShopDao;
import com.cloudkinto.entity.CompanyDo;
import com.cloudkinto.entity.ShopDo;
import com.cloudkinto.service.company.CompanyService;
import com.cloudkinto.service.company.vo.CompanyTree;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author 张永远
 * @since 2020-05-27
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class CompanyServiceImpl extends ServiceImpl<CompanyDao, CompanyDo> implements CompanyService {
    @Autowired
    private CompanyDao dao;
    @Autowired
    private ShopDao shopDao;

    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public Object add(Object req, Long userId) {
        CompanyDo entityDo = new CompanyDo();
        BeanUtils.copyProperties(req, entityDo);
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        return handlerAddAfter(entityDo);
    }

    //新增校验
    private void addCheck(Object req) {

    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(CompanyDo entityDo, Long userId) {
        entityDo.setCreateBy(userId);
        entityDo.setCreateTime(new Date());
        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }

    //将新增的do转换成要返回的实体
    private Object handlerAddAfter(CompanyDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success("");
    }

    @Override
    public Object update(Object req, Long userId) {
        /*CompanyDo entityDo = dao.selectById(req.getid());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);*/
        return null;
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(CompanyDo entityDo, Long userId) {
        entityDo.setUpdateBy(userId);
        entityDo.setUpdateTime(new Date());
    }

    //做修改之后要执行的业务定制处理
    private Object handlerUpdateAfter(CompanyDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        CompanyDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        return dao.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(CompanyDo entityDo, Long userId) {
        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_TRUE);
        entityDo.setDeleteBy(userId);
        entityDo.setDeleteTime(new Date());
    }

    @Override
    public Object detail(Long id) {
        CompanyDo entityDo = dao.selectById(id);
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private Object handlerDetailAfter(Object res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public SingleResult getList() {
        List<Map> mapList = new ArrayList<>();
        List<CompanyDo> companyDos = dao.selectList(null);
        if(!CollectionUtils.isEmpty(companyDos)){
            companyDos.forEach(companyDo -> {
                Map map = new HashMap();
                map.put("id",companyDo.getId());
                map.put("value",companyDo.getCompanyCode());
                map.put("name", companyDo.getName());
                mapList.add(map);
            });
        }
        return SingleResult.success(mapList);
    }

    @Override
    public List<CompanyTree> tree() {
        List<CompanyTree> treeList = new ArrayList<>();
        List<CompanyDo> companyDoList = dao.selectList(null);
        if(!CollectionUtils.isEmpty(companyDoList)){
            for (CompanyDo companyDo : companyDoList) {
                CompanyTree companyTree = new CompanyTree();
                companyTree.setId(companyDo.getId());
                companyTree.setName(companyDo.getCompanyCode());

                List<CompanyTree> childrenList = new ArrayList<>();

                LambdaQueryWrapper<ShopDo> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(ShopDo::getCompanyId,companyDo.getId());
                List<ShopDo> shopDoList = shopDao.selectList(wrapper);
                if(!CollectionUtils.isEmpty(shopDoList)){
                    for (ShopDo shopDo : shopDoList) {
                        CompanyTree children = new CompanyTree();
                        children.setId(shopDo.getId());
                        children.setName(shopDo.getShopName());

                        childrenList.add(children);
                    }
                    companyTree.setChildren(childrenList);
                }

                treeList.add(companyTree);
            }
        }
        return treeList;
    }

    @Override
    public List<Map> getInitList(Long companyId) {
        List<Map> list = new ArrayList<>();
        QueryWrapper wrapper2 = new QueryWrapper();
        if (companyId != null) {
            wrapper2.eq("id", companyId);
        }
        List<CompanyDo> companyDos = list(wrapper2);
        for (CompanyDo c :
                companyDos) {
            Map<String, String> map = new HashMap<>();
            map.put("id", c.getId() + "");
            map.put("value", c.getCompanyCode());
            list.add(map);
        }
        return list;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<CompanyDo> wrapper = queryBuild(map);
        IPage<CompanyDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<Object> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<Object> handlerListAfter(List<CompanyDo> list) {
        List<Object> result = new ArrayList<>();
        return result;
    }

    private IPage<CompanyDo> pageInit(Map<String, String> map) {
        IPage<CompanyDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<CompanyDo> queryBuild(Map<String, String> map) {
        QueryWrapper<CompanyDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("keyword", map.get("keyword"));
        }
        return wrapper;
    }


}
